package com.dzjcloud.dzj.gateway.controller;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSONArray;
import com.dzjcloud.dzj.common.core.utils.R;
import com.dzjcloud.dzj.common.redis.constant.RedisInfoConstant;
import com.dzjcloud.dzj.common.redis.utils.RedisUtil;
import com.dzjcloud.dzj.gateway.dto.Gateway;
import com.dzjcloud.dzj.gateway.service.impl.DynamicRouteServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;

/**
 * <h3>dzj-cloud</h3>
 * <p>网关前端控制器</p>
 *
 * @author : DongZeJian
 * @date : 2023-04-11 10:39
 **/
@RestController
@Slf4j
@RequestMapping
public class GatewayController {

    @Autowired
    private DynamicRouteServiceImpl dynamicRouteService;

    @Autowired
    private RedisUtil redisUtil;



    /**
     * 刷新网关
     *
     * @return
     */
    @GetMapping("/refresh")
    public R<String> refresh(){
        //获取路由信息
        String gatewayString=(String)redisUtil.get(RedisInfoConstant.GATEWAY_ROUTER_INFO_KEY);
        Validator.validateNotEmpty(gatewayString,"没有配置信息");
        List<Gateway> gateways = JSONArray.parseArray(gatewayString, Gateway.class);
        log.info("------当前路由信息：{}",gateways);
        dynamicRouteService.refresh(gateways);
        return R.ok();
    }
}
